package com.filetool.util;

import com.filetool.main.Main;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: Chy
 * @Description:
 * @Date: Created at 12:05 2018/3/13
 */
public class MyUtil {

    private static final int FLAVOR_TYPE = 18;  //18种规格

    /**
     * @Author: Chy
     * @Param:
     * @Description: 获取训练集中每天各种虚拟机出现的次数(只保存需要预测的虚拟机)
     * @Date: 12:12 2018/3/13
     */
    public static int[][] getFlavorNumByDay(String[] ecsContent) throws ParseException {
        //训练时间跨度
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String firstTrainDate = (ecsContent[0].split("\t"))[2];
        int count = 1;
        for (int i = 1; i < ecsContent.length; i++){
            if (ecsContent[i].contains("\t") && ecsContent[i].split("\t").length == 3){
                count++;
            }
        }
        String lastTrainDate = (ecsContent[count - 1].split("\t"))[2];
        Long trainStart = df.parse(firstTrainDate).getTime();
        Long trainEnd = df.parse(lastTrainDate).getTime();
        int trainDays = (int) Math.ceil((trainEnd - trainStart) * 1.0 / 86400000) + 1;

        Set<String> flavorNameSet = new HashSet<>();
        for (int i = 0; i < ecsContent.length; i++) {
            if (ecsContent[i].contains("\t") && ecsContent[i].split("\t").length == 3){
                String[] array = ecsContent[i].split("\t");
                String uuid = array[0];
                String flavorName = array[1];
                String createTime = array[2];
                flavorNameSet.add(flavorName);
            }
        }

        int[][] flavorNumByDay = new int[trainDays][FLAVOR_TYPE];

        for (int i =0; i < ecsContent.length; i++) {
            if (ecsContent[i].contains("\t") && ecsContent[i].split("\t").length == 3){
                String oneDayDate = ecsContent[i];
                String[] split = oneDayDate.split("\t");
                int index = (int) Math.floor((df.parse(split[2]).getTime() - trainStart) * 1.0 / 86400000); //第几天
                int flavorIndex = Integer.parseInt(split[1].substring(6));  //flavor几，例如flavor4，则flavorIndex=4
//            flavorIndex = flavorIndex > FLAVOR_TYPE ? FLAVOR_TYPE : flavorIndex;
                if (flavorIndex > FLAVOR_TYPE) continue;
                flavorNumByDay[index][flavorIndex - 1] = flavorNumByDay[index][flavorIndex - 1] + 1;
            }
        }

//        printArray(flavorNumByDay);

        return flavorNumByDay;
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 统计各种虚拟机累计出现的次数
     * @Date: 15:39 2018/3/15
     */
    public static int[][] getSumOfFlavor(int[][] flavorNumByDay){

        int[][] sum = (int[][]) CloneUtils.clone(flavorNumByDay);
        for (int j = 0; j < sum[0].length; j++){
            for (int i = 1; i < sum.length; i++){
                sum[i][j] = sum[i-1][j] + sum[i][j];
            }
        }
        return sum;
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 控制台打印二维数组
     * @Date: 15:46 2018/3/15
     */
    public static void printArray(int[][] array){
        //打印结果
        for (int i = 0; i < array.length; i++){
            StringBuffer sb = new StringBuffer("第" + (i+1) + "天：");
            for (int j = 0; j < array[i].length; j++){
                sb.append(array[i][j] + " ");
            }
            System.out.println(sb.toString());
        }
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 控制台打印二维数组
     * @Date: 15:46 2018/3/15
     */
    public static void printArray(double[][] array){
        //打印结果
        for (int i = 0; i < array.length; i++){
            StringBuffer sb = new StringBuffer("第" + (i+1) + "天：");
            for (int j = 0; j < array[i].length; j++){
                sb.append(array[i][j] + " ");
            }
            System.out.println(sb.toString());
        }
    }
}